package com.muyu.day4zy.service.impl;

import com.muyu.day4zy.domain.Goods;
import com.muyu.day4zy.domain.Sku;
import com.muyu.day4zy.domain.req.CalculateReq;
import com.muyu.day4zy.domain.result.Result;
import com.muyu.day4zy.mapper.GoodsMapper;
import com.muyu.day4zy.service.GoodsService;
import org.apache.poi.hpsf.Decimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName GoodsServiceImpl
 * @Description 描述
 * @Author ShiYu.Cui
 * @Date 2024/3/25 20:00
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查询商品列表
     * @return
     */
    @Override
    public Result<List<Goods>> goodsList() {
        List<Goods> list=goodsMapper.goodsList();
        return Result.success(list);
    }

    /**
     * 查询sku列表
     * @param id
     * @return
     */
    @Override
    public Result<List<Sku>> skuList(Integer id) {
        List<Sku> skuList=goodsMapper.skuListById(id);
        return Result.success(skuList);
    }

    /**
     * 计算
     * @param calculateReq
     * @return
     */
    @Override
    public Result calculate(CalculateReq calculateReq) {
        List<Sku> listResult=goodsMapper.skuListById(calculateReq.getGid());
        for (Sku sku : listResult) {
            if(calculateReq.getId().equals(sku.getId()) && calculateReq.getNum() >= sku.getNum()){
                return Result.error("sku不足");
            }
        }
        Integer num = calculateReq.getNum();
        Integer vip = calculateReq.getVip();
        Double price = calculateReq.getPrice();
        Double postage = calculateReq.getPostage();

        Double sum = num*price+postage;
        if(vip==1){
            sum=sum*0.9;
        }
        return Result.success(sum);
    }
}
